package com.ashin.leetcode;

import org.junit.Test;

/**
 * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。
 *
 * @author Ashin
 */
public class L003LengthOfLongestSubstring {


    @Test
    public void test() {
        System.out.println("sss".substring(0, 0));
        System.out.println(lengthOfLongestSubstring("xabcabcd"));
        System.out.println(t("bbbbb"));
    }

    public int lengthOfLongestSubstring(String s) {
        if (s.length() <= 1) {
            return s.length();
        }

        int a = lengthOfLongestSubstring(s.substring(1)) + (s.substring(1).contains(s.substring(0, 1)) ? 0 : 1);

        int b = lengthOfLongestSubstring(s.substring(0, s.length() - 1)) + (s.substring(0, s.length() - 1).contains(s.substring(s.length() - 1)) ? 0 : 1);

        return a > b ? a : b;
    }

    public int t(String s) {
        if (s == null || s.length() <= 0) {
            return 0;
        }
        int max = 1;
        int len = 1;
        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 1; j < s.length(); j++) {
                if (!s.substring(i, j).contains(s.charAt(j) + "")) {
                    len++;
                } else {
                    break;
                }
            }

            max = Math.max(max, len);
            len = 1;
        }

        return max;
    }

}
